package cn.edu.qlnu.xky.test.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.edu.qlnu.xky.test.common.Page;
import cn.edu.qlnu.xky.test.model.Student;

/**
 * 本类是具体到某模块的
 * 不可以出现jdbc的内容（连接字符串，jdbc对象）
 * 
 * 可以出现sql语句，本类是将service传递来的参数转换为sql语句，调用BaseDao的方法执行
 * 
 */
public class StudentDao {

	BaseDao dao = null;
	
	public StudentDao() {
		dao = new BaseDao();
	}
	
	public int addStudent(Student stu) {
		//将对象中的数据，附加到insert sql中，调用BaseDao的方法
		String sql = "insert into t_student(stuName,psw,gender,birthday,hobby,school,temp)"
				+ "values('"
				+stu.getUserName()+"','"
				+stu.getPsw()+"','"
				+stu.getGender()+"','"
				+stu.getBirthdayYMDStr()+"','"
				+stu.getHobbiesKeyStr()+"','"
				+stu.getSchool()+"','"
				+stu.getTemp()+"')";
		return dao.doAddEditDelete(sql);
	}
	
	public int edtiStudent(Student stu) {
		String sql = "update t_student set "
				+ "stuName='"+stu.getUserName()+"',"
				+ "gender='"+stu.getGender()+"',"
				+ "birthday='"+stu.getBirthdayYMDStr()+"',"
				+ "hobby='"+stu.getHobbiesKeyStr()+"',"
				+ "school='"+stu.getSchool()+"',"
				+ "temp='"+stu.getTemp()+"' "
				+ "where id="+stu.getId();
		return dao .doAddEditDelete(sql);
	}
	
	public int deleteStudent(int id) {
		String sql = "delete from t_student where id="+id;
		return dao.doAddEditDelete(sql);
	}
	
	public Student searchById(int id) {
		String sql = "select * from t_student where id="+id;
		List<Student> stus = this.searchBySql(sql);
		if(stus.size() == 0)
			return new Student();
		else
			return stus.get(0);
	}
	
	private List<Student> searchBySql(String sql){
		List<Student> stus = new ArrayList<Student>();
		List<Map<String,Object>> objs = dao.searchBySql(sql);
		Student stu = null;
		for(Map<String,Object> obj : objs) {
			stu = new Student();
			stu.setBirthday((Date)obj.get("birthday"));
			stu.setGender((String)obj.get("gender"));
			String[] hobby = ((String)obj.get("hobby")).split(" ");
			stu.setHobbies(hobby);
			stu.setId((int)obj.get("id"));
			stu.setPsw((String)obj.get("psw"));
			stu.setSchool((String)obj.get("school"));
			stu.setTemp((String)obj.get("temp"));
			stu.setUserName((String)obj.get("stuName"));
			
			stus.add(stu);
		}
		return stus;
	}
	
	public Page<Student> searchByNamePage(String name,int pageNo){
		
		String sqlWhere=" where stuName like '%"+name+"%'";
		String sql = "select * from t_student "+sqlWhere;
		Page<Student> p = new Page<Student>();
		p.setPageNo(pageNo);//1
		List<Student> stus = new ArrayList<Student>();
		
		List<Map<String,Object>> objs = dao.searchByPage(sql,pageNo);
		Student stu = null;
		for(Map<String,Object> obj : objs) {
			stu = new Student();
			stu.setBirthday((Date)obj.get("birthday"));
			stu.setGender((String)obj.get("gender"));
			String[] hobby = ((String)obj.get("hobby")).split(" ");
			stu.setHobbies(hobby);
			stu.setId((int)obj.get("id"));
			stu.setPsw((String)obj.get("psw"));
			stu.setSchool((String)obj.get("school"));
			stu.setTemp((String)obj.get("temp"));
			stu.setUserName((String)obj.get("stuName"));
			
			stus.add(stu);
		}
		p.setRecords(stus);//3ok
		
		String sqlCount = "select count(*) from t_student "+sqlWhere;
		int count = new BaseDao().searchCount(sqlCount);
		p.setTotalCount(count);//2ok
		return p;
	}
	
	public List<Student> searchByName(String name){
		String sql = "select * from t_student where stuName like '%"+name+"%'";
		return this.searchBySql(sql);
	}
}
